<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title></title>
</head>
<body>
<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-username"></i>
            </div>
            <select name="parkId">
                <option value="">请选择停车场</option>

            </select>
        </div>
    </div>

    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-date"></i>
            </div>
            <input type="text" name="inTime" readonly placeholder="入场时间" class="layui-input demo-table-search-date">
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-date"></i>
            </div>
            <input type="text" name="outTime" readonly placeholder="出场时间" class="layui-input demo-table-search-date">
        </div>
    </div>
    <div class="layui-btn-container layui-col-xs12">
        <button class="layui-btn" lay-submit lay-filter="demo-table-search">Search</button>
        <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
    </div>
</form>

<button class="layui-btn" lay-on="e1">入场出场</button>
<table class="layui-hide" id="ID-table-demo-search"></table>
<div style="display: none" id="inForm">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">停车场</label>
            <div class="layui-input-block">
                <select name="parkId">
                    <option value="">请选择停车场</option>

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车牌号</label>
            <div class="layui-input-block">
                <input type="text" name="plateNumber" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>

    </div>
</script>

<script>
    layui.use(function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var util = layui.util;
        //  2.9+ 版本可省略 attr 参数，默认读取 lay-on
        util.on({
            e1: function () {
                layer.open({
                    type: 1, // page 层类型
                    area: ['500px', '300px'],
                    title: '车辆入场',
                    shade: 0.6, // 遮罩透明度
                    shadeClose: true, // 点击遮罩区域，关闭弹层
                    maxmin: true, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content: $("#inForm")
                });
            },

        });

        //获取所有的停车场
        $.get("/park/list.do", {page: 1, limit: 100}, function (res) {
            let data = res.data.list;
            data.map(item => {
                $("[name='parkId']").append("<option value='" + item.id + "'>" + item.parkName + "</option>")
            });
            form.render('select')
        })
        // 创建表格实例
        table.render({
            elem: '#ID-table-demo-search',
            url: '/record/list.do', // 此处为静态模拟数据，实际使用时需换成真实接口
            parseData: function (res) {
                return {
                    code: res.code,
                    data: res.data.list,
                    count: res.data.total,
                }
            },
            limit: 3,
            cols: [[
                {type: 'radio', title: '😊', fixed: true}, // 单选框
                {field: 'id', title: 'ID', width: 30, sort: true, fixed: true},
                {field: 'parkName', title: '停车场名称', width: 150},
                {field: 'plateNumber', title: '车牌号', width: 110, sort: true},

                {field: 'cardType', title: '类型', width: 80},
                {field: 'inTime', title: '入场时间', width: 180},
                {field: 'outTime', title: '出场时间', width: 180},
                {field: 'parkingDuration', title: '停车时长', width: 150},
                {field: 'fee', title: '停车费用', width: 100},
                {fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#toolDemo'}
            ]],
            page: true,
            height: 310
        });
        // 日期
        laydate.render({
            elem: '.demo-table-search-date'
        });

        // 搜索提交
        form.on('submit(demo1)', function (data) {
            var field = data.field; // 获得表单字段
            $.post("/record/addOrUpdate.do", field, function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {time: 2000}, function () {
                        layer.closeAll();//关闭弹出层
                        table.reload('ID-table-demo-search', {
                            page: {
                                curr: 1 // 重新从第 1 页开始
                            },

                        });
                    })
                } else {
                    layer.msg(res.msg, {time: 2000});
                }
            })
            return false; // 阻止默认 form 跳转
        });


        // 搜索提交
        form.on('submit(demo-table-search)', function (data) {
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('ID-table-demo-search', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
            });
            layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
            return false; // 阻止默认 form 跳转
        });

        table.on('tool(ID-table-demo-search)', function (obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            console.log(data)
            if (obj.event === 'edit') {
                layer.open({
                    title: '编辑 - id:' + data.id,
                    type: 1,
                    area: ['60%', '60%'],
                    content: $("#addForm"),
                    success: function () {
                        //为表单赋值
                        form.val("addForm", data);
                        //渲染表单
                        form.render();
                    }
                });
            }
        })
    });
</script>

</body>
</html>